#include<cstdio>
#include<sys/wait.h>
#include<sys/types.h>
#include<unistd.h>
#include<cstdlib>
#include<functional>
#include<vector>
#include "task.h"
#include<iostream>

typedef std::function<void()> task_t;

void LoadTask(std::vector<task_t> &tasks)
{
    tasks.push_back(Printlog);
    tasks.push_back(Download);
    tasks.push_back(Backup);
}

int main()
{
    std::vector<task_t> tasks;
    LoadTask(tasks);
    pid_t id = fork();
    if(id == 0)
    {
        //child
        while(true)
        {
            printf("我是子进程，pid : %d\n",getpid());
            sleep(1);
        }
        exit(0);
    }

    //father
    while(true)
    {
        sleep(1);
        pid_t rid = waitpid(id, nullptr, WNOHANG);
        if(rid > 0)
        {
            printf("等待子进程%d 成功\n",rid);
            break;
        }
        else if(rid < 0)
        {
            printf("等待子进程失败\n");
            break;
        }
        else
        {
            printf("子进程尚未退出\n");
            for(auto& task : tasks)
            {
               task();
            }
        }
    }
    return 0;
}
